package com.ukao.cashregister.ui.setting;


import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.ukao.cashregister.R;
import com.ukao.cashregister.base.BaseFragment;
import com.ukao.cashregister.utils.CheckUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportFragment;


public class SetTingFragment extends BaseFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.back_layout)
    LinearLayout backLayout;
    @BindView(R.id.user_info_toolbar)
    Toolbar userInfoToolbar;
    @BindView(R.id.setting_store)
    RadioButton settingStore;
    @BindView(R.id.setting_wifi)
    RadioButton settingWifi;
    @BindView(R.id.setting_link)
    RadioButton settingLink;
    @BindView(R.id.setting_scan_acode)
    RadioButton settingScanAcode;
    @BindView(R.id.setting_stamp)
    RadioButton settingStamp;
    @BindView(R.id.setting_camera)
    RadioButton settingCamera;
    @BindView(R.id.setting_local)
    RadioButton settingLocal;
    @BindView(R.id.setting_upgrade)
    RadioButton settingUpgrade;
    @BindView(R.id.set_layout)
    FrameLayout setLayout;
    @BindView(R.id.radio_group)
    RadioGroup radioGroup;


    private String mParam1;
    private String mParam2;

    private Unbinder unbinder;
    private SupportFragment[] mFragments = new SupportFragment[8];
    private int defSelecePos = 0;


    public static SetTingFragment newInstance(int defSelecePos, String param2) {
        SetTingFragment fragment = new SetTingFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, defSelecePos);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            defSelecePos = getArguments().getInt(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_set_ting, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.back_layout, R.id.user_info_toolbar})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_layout:
                finish();
                break;
            case R.id.user_info_toolbar:
                break;
        }
    }


    @Override
    protected void initView() {
        SupportFragment firstFragment = findChildFragment(StoreNameFragment.class);
        if (firstFragment == null) {
            mFragments[0] = StoreNameFragment.newInstance("", "");
            mFragments[1] = WifiListFragment.newInstance("", "");
       /*     mFragments[2] = WiredNetworkFragment.newInstance("","");*/
//            mFragments[2] = ParameterFragment.newInstance("", "");
            mFragments[2] = PrintRobFragment.newInstance("", "");
//            mFragments[4] = ParameterFragment.newInstance("", "");
            mFragments[3] = AboutLocalFragment.newInstance("", "");
            mFragments[4] = VersionFragment.newInstance("", "");
            mFragments[5] = ParameterFragment.newInstance("", "");
            loadMultipleRootFragment(R.id.set_layout, defSelecePos,
                    mFragments[0],
                    mFragments[1],
                    mFragments[2],
                    mFragments[3], mFragments[4], mFragments[5]);

        } else {
            mFragments[0] = firstFragment;
            mFragments[1] = findChildFragment(WifiListFragment.class);
     /*       mFragments[2] = findChildFragment(WiredNetworkFragment.class);*/
//            mFragments[2] = findChildFragment(ParameterFragment.class);
            mFragments[2] = findChildFragment(PrintRobFragment.class);
            mFragments[3] = findChildFragment(AboutLocalFragment.class);
            mFragments[4] = findChildFragment(VersionFragment.class);
            mFragments[5] = findChildFragment(ParameterFragment.class);
        }
        switch (defSelecePos) {

            case 1:
                settingWifi.setChecked(true);

                break;

        }

    }


    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        if (!CheckUtils.isMIUIDevices()){
            Beta.checkUpgrade(false,false);
        }

    }

    @OnCheckedChanged({R.id.setting_store, R.id.setting_wifi, R.id.setting_link, R.id.setting_scan_acode, R.id.setting_stamp, R.id.setting_camera,
            R.id.setting_local, R.id.setting_upgrade,R.id.setting_parameter})
    public void OnCheckedChanged(CompoundButton view, boolean check) {

        if (check) {
            switch (view.getId()) {
                case R.id.setting_store:
                    showHideFragment(mFragments[0]);
                    break;
                case R.id.setting_wifi:
                    showHideFragment(mFragments[1]);
                    break;
                case R.id.setting_link:
                    // showHideFragment(mFragments[2]);
                    break;
                case R.id.setting_scan_acode:

                  //  showHideFragment(mFragments[2]);
                    break;
                case R.id.setting_stamp:
                    showHideFragment(mFragments[2]);
                    break;
                case R.id.setting_camera:


                 //  showHideFragment(mFragments[4]);
                    break;
                case R.id.setting_local:
                    showHideFragment(mFragments[3]);

                    break;
                case R.id.setting_upgrade:
                    Beta.checkUpgrade(false, false);
                    showHideFragment(mFragments[4]);
                    break;
                case  R.id.setting_parameter:
                    showHideFragment(mFragments[5]);
                    break;
            }
        }
    }


}
